//
//  NetManager.m
//  优学荟管理系统
//
//  Created by 星道 on 17/6/26.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "NetManager.h"
#import "NetMrg.h"

@implementation NetManager

+ (void)Get:(NSString *)UrlStr Auth:(BOOL)Auth Params:(NSDictionary *)Params Success:(void (^)(id))Success Fail:(void (^)(NSError *))Fail
{
    [NetMrg Get:UrlStr Auth:Auth Params:Params Success:Success Fail:Fail];
}

+ (void)Post:(NSString *)UrlStr Auth:(BOOL)Auth Params:(NSDictionary *)Params Success:(void (^)(id))Success Fail:(void (^)(NSError *))Fail
{
    [NetMrg Post:UrlStr Auth:Auth Params:Params Success:Success Fail:Fail];
}

+ (void)UpdataImgWithURL:(NSString *)url Img:(UIImage *)Img parameter:(NSDictionary *)paraDic success:(void (^)(id obj))success fail:(void (^)(NSError *error))fail
{
    [NetMrg UpdataImgWithURL:url pic:Img parameter:paraDic success:success fail:fail];
}

+ (void)UpdataAudioWithURL:(NSString *)url FilePath:(NSString *)FilePath parameter:(NSDictionary *)paraDic success:(void (^)(id obj))success fail:(void (^)(NSError *error))fail
{
    [NetMrg UpdataAudioWithURL:url FilePath:FilePath parameter:paraDic success:success fail:fail];
}

+ (void)UpdataImgsWithURL:(NSString *)url imgs:(NSArray <UIImage *>*)imgs parameter:(NSDictionary *)paraDic Progress:(void (^)(NSProgress * _Nonnull uploadProgress))Progress success:(void (^)(id obj))success fail:(void (^)(NSError *error))fail
{
    [NetMrg UpdataImgsWithURL:url imgs:imgs parameter:paraDic Progress:Progress success:success fail:fail];
}

/** 下载 */
+ (void)Down:(NSString *)UrlStr Auth:(BOOL)Auth Params:(NSDictionary *)Params completionHandler:(void (^)(NSURLResponse *response, NSURL *filePath, NSError *error))completionHandler {

    [NetMrg Down:UrlStr Auth:Auth Params:Params completionHandler:completionHandler];
}
+ (void)DownLoadFileWithUrlStr:(NSString *)UrlStr FileName:(NSString *)FileName Path:(NSString *)Path Progress:(void(^)(CGFloat))Progress Complete:(void(^)(NSString *FullPath,NSError *Err))Complete
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSURL *url = [NSURL URLWithString:UrlStr];
    
    
    
    
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    /*
     第一个参数:请求对象
     第二个参数:progress 进度回调
     第三个参数:destination--(downloadTask-)
     在该block中告诉AFN应该把文件存放在什么位置,AFN内部会自动的完成文件的剪切处理
     targetPath:文件的临时存储路径(tmp)
     response:响应头信息
     返回值:文件的最终存储路径
     第四个参数:completionHandler 完成之后的回调
     filePath:文件路径 == 返回值
     */
    NSURLSessionDownloadTask *download = [manager downloadTaskWithRequest:request progress:
                                          ^(NSProgress * _Nonnull downloadProgress) {
                                              
                                              //进度回调,可在此监听下载进度(已经下载的大小/文件总大小)
                                              Progress (1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);
                                              
                                          } destination:^NSURL * _Nonnull(NSURL * _Nonnull
                                                                          targetPath, NSURLResponse * _Nonnull response) {
                                              NSFileManager *fileManeger = [NSFileManager defaultManager];
                                              if (![fileManeger fileExistsAtPath:Path])
                                              {
                                                  [fileManeger createDirectoryAtPath:Path
                                                         withIntermediateDirectories:YES
                                                                          attributes:nil
                                                                               error:nil];
                                              }
                                              //存储路径
                                              return [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@",Path,FileName]];
                                              
                                          } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath,
                                                                NSError * _Nullable error) {
                                              
                                              //下载成功返回保存的路径
                                              Complete (filePath.absoluteString,error);
                                              
                                          }];
    
    [download resume];
}

@end
